/***************************************************************************
 创建者: 华磊
 开始时间: 2018.8.14
 copyright: (C) 华友高科
 修改说明: (每次有修改就添加一条，带有 修改人，修改时间，修改描述)
 example (1) hualei 2017.5.8 类的具体实现代码编写

 ***************************************************************************
 *  @file x.h
 *  功能:
 *
 *                                                                         *
 ***************************************************************************/
#ifndef SAFETYFUNCTION_H
#define SAFETYFUNCTION_H

#include <vector>
#include <string>
#include <QString>
#include <QVector>
#include "GeneralDefine.h"
#include <QMap>

#define SAFETY_SETTING_FILE_PATH "globalConfig/safetyCheckInfo/safetyCheckInfo.ini"
#define ULTROSONIC_SENSOR_FILE_PATH "globalConfig/ultrasonicSensor/ultrasonicSensor.xml"
#define D_SLOWDOWN_SIZE 4

class UltrasonicSensor;
/**
 * @brief The SafetyFunction class 安全管理类
 */
class SafetyFunction
{
public:
    SafetyFunction();

public:
    int setSefetyCheck(E_SAFETY_CHECK typeIn,int isEnable);
    int getSefetyCheck(E_SAFETY_CHECK typeIn,int &checkStatusReturn);
    int setUltrasonicSensorDebugFlag(int debugFlagIn);
    int getUltrasonicSensorName(std::vector<std::string> &sensorNames);
    int setUltrasonicSensor485Address(int origineAddressIn, int addressIn);
    int setUltrasonicSensorCheckEnable(bool isOn);
    int getUltrasonicSensorSafeStatus(std::vector<double> &safeStatusOut,bool &isCheckEnableOut);
    int setUltrasonicPowerSupplyNoiseClass(int classIn);
    int setUltrasonicWork(int locationIn,bool isEnable);
    int getUltrasonicDetectDistance(QVector<double> &distanceResultOut);
    int getUltrasonicDetectDistance(int locationIn,QVector<double> &distanceResultOut);
    int getUltrasonicStatus(QVector<int> &deviceRequestCountOut);

    int ultrasonicCheck();
    int motorBatteryCheck();
    int powerLoseCheck();
    int startSafetyCheck();
    int stopSafetyCheck();
//    int resetEmergencyStop();
    int safetyCheckLoop();
//    int motorServoOnMonitor();
    int emergencyStopDiMonitor();
    int laserMonitor();
    int errorMessageMonitor();
    int hmiServoOnDiMonitor();

    int systemInitialCheck();




private:

    int autoStartUpServoOn();
    int systemDoOutput();
    int systemDiMonitor();
    int slowDownDiMonitor();
    int enableRunDiMonitor();
    int writeSettingFile(QString settingFile);
    /**
     * @brief readSettingFile 注意：没有做文件内容是否缺省的判断
     * @param settingFile
     * @return
     */
    int readSettingFile(QString settingFile);
    void addMsg( int messageLevel,
                 std::string componentName,
                 std::string messageType,
                 int messageCode,
                 int robotId,
                 int parameter1 = 0,
                 int parameter2 = 0,
                 int parameter3 = 0,
                 int parameter4 = 0 );

    void* threadRun(void *);
    static void* threadTask( void* classPtr );
    void createThread();
    void realTimeSleep(long long umicoroSecond);

private:
    QString configFilePath;
    pthread_t threadId;
    bool safetyCheckFlag;
    bool emergencyStopStatus;
    int powerCheckFlag;
    int motorBatteryCheckCount;

    QVector<int> slowDownFlag;
    QVector<double> slowDownTime;
    QVector<double> slowDownRatio;
    QVector<bool> diOld_slow;
    QVector<bool> isSlowDownState;

    int enableRun1Flag;
    int enableRun2Flag;
    int enableRun3Flag;
    int enableRun4Flag;
    bool initialedEnableRun1;
    bool initialedEnableRun2;
    bool initialedEnableRun3;
    bool initialedEnableRun4;

    int resetAlarmFlag;
    int extServOnFlag;
    int stopFlag;
    int ceaseFlag;
    int runFlag;
    int isEnableRun_autoMode;
    int emergencyStopMonitorFlag;
    int hmiServoDiMonitorFlag;
    int autoStartUpServOnFlag;
    bool isStartUpServoOnFinished;
    UserInfo userInfo;
    UltrasonicSensor *ultrasonicSensor;
    int ultrasonicCheckFlag;
    int runSafetyCheck1Flag;//not used
    int runSafetyCheck2Flag;
    int runSafetyCheck3Flag;
    int runSafetyCheck4Flag;

    bool isErrorStatus;
    bool isPlayErrorMusic;
    bool isPlayBarrierMusic;
    int laser1MonitorFlag;//小于０代表不检查，大于等于０代表dｉ号码
    int laser2MonitorFlag;
    bool laserOkValue;//sick 是npn输出，如果不用继电器转换，则正常状态是０，异常状态是１
    bool isInitialOkStatus;



};

#endif // SAFETYFUNCTION_H
